package leetcode;

import util.DoubleListNode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0707.java
 * @Author : WangRuoyu
 * @Date 2023/11/7 21:35
 */


public class Q0707 {
    DoubleListNode head = new DoubleListNode(-1);
    DoubleListNode tail = new DoubleListNode(-1);
    int count = 0;

    //    public MyLinkedList() {
    public Q0707() {
        head.next = tail;
        tail.pre = head;
    }

    public int get(int index) {
        if (index >= count) return -1;
        DoubleListNode p = head.next;
        while (index-- > 0) {
            p = p.next;
        }
        return p.val;
    }

    public void addAtHead(int val) {
        DoubleListNode p = new DoubleListNode(val, head, head.next);
        head.next.pre = p;
        head.next = p;
        count++;
    }

    public void addAtTail(int val) {
        DoubleListNode p = new DoubleListNode(val, tail.pre, tail);
        tail.pre.next = p;
        tail.pre = p;
        count++;
    }

    public void addAtIndex(int index, int val) {
        if (index > count) return;
        DoubleListNode p = head.next;
        while (index-- > 0) {
            p = p.next;
        }
        DoubleListNode s = new DoubleListNode(val, p.pre, p);
        p.pre.next = s;
        p.pre = s;
        count++;
    }

    public void deleteAtIndex(int index) {
        if (index >= count) return;
        DoubleListNode p = head;
        while (index-- > 0) {
            p = p.next;
        }
        p.next = p.next.next;
        p.next.pre = p;
        count--;
    }

    public static void main(String[] args) {
        Q0707 sl = new Q0707();
    }
}